package jvm;

import sun.misc.Unsafe;

import java.nio.channels.AsynchronousChannel;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {

    private int a=1;

    private void op2(){
        while (true){
            /**
             * 如果输入a,那么字节码会有 load指令 加载a,加载就会更新a的指;
             * 不给a加volatile关键字也没事。
             */
//            System.out.println(a);
            if(a==2) {
                System.out.println("你好");
            }
//            try {
//                Thread.sleep(100);//sleep也会,可能是唤醒时会重新从常量池中读取值
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Test test=new Test();

        //3.
        Thread thread=new Thread(()->{
            test.op2();
        });
        thread.start();

        Thread.sleep(1500);
        test.a=2;
        thread.join();

        //乐观锁
        AtomicReference atomicReference=new AtomicReference();

    }

}
